package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 云南信息港解析
 * @author Persh
 *
 */
public class YninfoNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.yninfo.com/.*/[0-9]{6}/t[0-9]{8}_[0-9]*.htm",
		"http://.*.yninfo.com/.*/[0-9]{6}/t[0-9]{8}_[0-9]*.html",
		"http://auto.yninfo.com/.*/read-[0-9]*-[0-9]*.html",
		"http://yc.yninfo.com/.*/.*-[0-9]{5}.html"};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		doc.select("div.Head").remove();
		doc.select("div.header").remove();
		doc.select("div.relative-news").remove();
		doc.select("div.second-list").remove();
		doc.select("div.P_List").remove();
		doc.select("div.P_Right").remove();
		doc.select("div.L_Pic").remove();
		doc.select("div.b-share").remove();

		title =doc.select("div.ync-content>h2").text();
		if (StringUtils.isBlank(title)){
			title =doc.select("div.L_Txt>h2").text();
			if(title==null || title.trim().length()==0){
				title =doc.select("div.news21_1").text();
				if(title==null || title.trim().length()==0){
					title =doc.select("div.right_center>h3").text();
					if(title==null || title.trim().length()==0){
						title =doc.select("h1").text();
						if(title==null || title.trim().length()==0){
							title =doc.select("div.readbt").text();
							if(title==null || title.trim().length()==0){
								title =doc.select("td.title>div>strong").text();
									if(title==null || title.trim().length()==0){
										title =doc.select("div.ReadPic>h2").text();
										if(title==null || title.trim().length()==0){
											title =doc.select("div.Wbt").text();
											if(title==null || title.trim().length()==0){
												title =doc.select("title").text();
												title=StringUtils.substringBefore(title, "---");
											}
										}
									}
							}
						}
					}
				}
			}
		}

			content=HtmlCleaner.getContentHtml(url,doc.select("div.word"));
			if(content==null || content.trim().length()==0){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.news21_3_main"));
				if(content==null || content.trim().length()==0){
					content=HtmlCleaner.getContentHtml(url,doc.select("div#contents.detail"));
					if(content==null || content.trim().length()==0){
						content=HtmlCleaner.getContentHtml(url,doc.select("td#content.read"));
						if(content==null || content.trim().length()==0){
							content=HtmlCleaner.getContentHtml(url,doc.select("div.article>p"));
							if(content==null || content.trim().length()==0){
								content=HtmlCleaner.getContentHtml(url,doc.select("div.arc-body"));
								if(content==null || content.trim().length()==0){
									content=HtmlCleaner.getContentHtml(url,doc.select("div.TRS_Editor"));
								}
							}
						}
					}
				}
			}
			content=content.replace("正在加载中... ", "");

			date=DateUtils.matchDate(doc.select(":containsOwn(时间：)").text());
			if(date==null ){

				date=DateUtils.matchDate(doc.select("div.TXT_00").text());
				if(date==null ){
					date=DateUtils.matchDate(doc.select("div.news21_2").text());
					if(date==null ){
						date=DateUtils.matchDate(doc.select("div.time").text());
						if(date==null ){
							date=DateUtils.matchDate(doc.select("span.txt01").text());
							if(date==null ){
								date=DateUtils.matchDate(doc.select("div.readtime>span").text());
								if(date==null ){
									date=DateUtils.matchDate(doc.select("div.green").text());
									if(date==null ){
										date=DateUtils.matchDate(doc.select("div.arcinfo>span").text());
										if(date==null ){
											date=DateUtils.matchDate(doc.select("div.Whitelink4").text());
											if(date==null ){
												date=DateUtils.matchDate(doc.select("p.time").text());
											}
										}
									}
								}
							}
						}
					}
				}
			}
			String author=JSoupUtils.matchAuthor(doc, "来源：");
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);
			news.setAuthor(author);

		return news;
	}

	public static void main(String[] args) {
		YninfoNewsAnalyse analyse=new YninfoNewsAnalyse();
		String url="http://news.yninfo.com/china/201405/t20140512_2248737.html";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
